package user

import (
	"git.oschina.net/fuxiaohei/fuxiaohei-go.git/Core"
	"git.oschina.net/fuxiaohei/fuxiaohei-go.git/Rt"
	"git.oschina.net/fuxiaohei/fuxiaohei-go.git/App/admin"
)

type Admin struct {

}

func (_ Admin) Setup() {
	Core.SetRoute("GET:/admin/users/add", "[I]user.Admin.AddPageAction")
	Core.SetRoute("POST:/admin/users/add", "[I]user.Admin.AddPostAction")
	Core.SetRoute("GET:/admin/users/edit", "[I]user.Admin.EditPageAction")
	Core.SetRoute("POST:/admin/users/edit", "[I]user.Admin.EditPostAction")
	Core.SetRoute("GET:/admin/users", "[I]user.Admin.ListPageAction")
}

// users list page
func (_ Admin) ListPageAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	data := make(map[string]interface {})
	data["Users"] = GetList()
	response.Body = admin.RenderAdminPage("user/list.html", data, nil, "user")
}

// add user page
func (_ Admin) AddPageAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	response.Body = admin.RenderAdminPage("user/add.html", nil, nil, "user")
}

// edit user page
func (_ Admin) EditPageAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	// check param and get user id by param
	if len(request.Param) < 6 {
		response.Redirect("/admin/users/")
		return
	}
	id := request.Param[4]
	data := make(map[string]interface {})
	data["User"] = GetOneById(id)
	response.Body = admin.RenderAdminPage("user/edit.html", data, nil, "user")
}

// edit user post action
func (_ Admin) EditPostAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	if !request.IsAjax() {
		response.Status = 400
		return
	}
	if len(request.Param) < 6 {
		response.Redirect("/admin/users/")
		return
	}
	id := request.Param[4]
	data := request.Input().All()
	res := map[string]interface {}{"res":false}
	// validate data
	msg := validateUserData(data)
	// if changing password, validate password string
	if data["password"] != "" {
		if Rt.Validator.IsShorter(data["password"], 7) {
			msg = "密码必须大于6位"
		}
	}
	if msg != "" {
		res["msg"] = msg
		response.Json(res)
		return
	}
	// update user data
	UpdateUser(id, data)
	// update user password
	if data["password"] != "" {
		UpdatePassword(id, data["password"])
	}
	res["res"] = true
	response.Json(res)
	return
}

// add new user post action
func (_ Admin) AddPostAction(request *Core.HxRequest, response *Core.HxResponse) {
	if !admin.LoginCheck(request) {
		admin.LoginDestroy(response)
		return
	}
	if !request.IsAjax() {
		response.Status = 400
		return
	}
	data := request.Input().All()
	res := map[string]interface {}{"res":false}
	msg := validateUserData(data)
	if Rt.Validator.IsShorter(data["password"], 7) {
		msg = "密码必须大于6位"
	}
	// check email is existed
	if checkEmailExist(data["email"]) {
		msg = "邮箱已经存在"
	}
	if msg != "" {
		res["msg"] = msg
		response.Json(res)
		return
	}
	AddUser(data)
	res["res"] = true
	response.Json(res)
	return
}
